import 'dart:convert';
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import '../provider/person_info.dart';
import 'list_view.dart';
import 'title_table.dart';
import 'indicator.dart';
import 'package:http/http.dart' as http;
import '../provider/person_info.dart';

// ignore: must_be_immutable
class Show extends StatefulWidget {
  const Show({super.key});

  @override
  State<StatefulWidget> createState() => _Show();
}

class _Show extends State<Show> {
  var selectvalue = "";
  var listTitle = [];
  var listValue = [];
  int signCount = 0;
  List<dynamic> actlist = [];
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      final personInfo = Provider.of<PersonInfo>(context, listen: false);

      var url = Uri.parse('http://127.0.0.1:5000/act_name');
      var headers = {'Content-Type': 'application/json;charset=utf-8'};
      var body = jsonEncode({
        'alliance': personInfo.alliance,
      });
      final request = http.Request('get', url);
      request.headers.addAll(headers);
      request.body = body;
      request.send().then((value) {
        http.Response.fromStream(value).then((response) {
          setState(() {
            actlist = jsonDecode(response.body)["msg"];
          });
          setselectvalue(personInfo, actlist[actlist.length - 1]);
        });
      }).catchError((value) {
        Fluttertoast.showToast(msg: '初始化异常');
      });
    });
  }

  void setselectvalue(PersonInfo person, String newValue) {
    setState(() {
      selectvalue = newValue;
    });
    final url = Uri.parse('http://127.0.0.1:5000/act_list');
    final headers = {'Content-Type': 'application/json;charset=utf-8'};
    final body = jsonEncode({'alliance': person.alliance, "act": selectvalue});
    final request = http.Request('get', url);
    request.headers.addAll(headers);
    request.body = body;
    request.send().then((value) {
      http.Response.fromStream(value).then((response) {
        var list = jsonDecode(response.body)["msg"];
        var listValueUpdate = [];
        var listTitleUpdate = [];
        var count = 0;
        list.forEach((map) {
          map.forEach((key, value) {
            listTitleUpdate.add(key);
            if (value == "1") {
              listValueUpdate.add("✔");
              count = count + 1;
            } else if (value == "请假") {
              listValueUpdate.add("请假");
            } else {
              listValueUpdate.add("✘");
            }
          });
        });
        setState(() {
          listTitle = listTitleUpdate;
          listValue = listValueUpdate;
          signCount = ((count / listTitleUpdate.length) * 100).toInt();
        });
      });
    }).catchError((value) {
      Fluttertoast.showToast(msg: '初始化异常');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('展示'),
        ),
        body: Consumer<PersonInfo>(builder: (ctx, person, child) {
          return SizedBox(
              height: double.infinity,
              width: double.infinity,
              child: Center(
                  child: Column(
                children: [
                  Indicator(count: signCount),
                  TitleTable(
                    person: person,
                    actlist: actlist,
                    selectvalue: selectvalue,
                    setVaule: setselectvalue,
                  ),
                  Listview(
                      person: person,
                      selectvalue: selectvalue,
                      listTitle: listTitle,
                      listValue: listValue)
                ],
              )));
        }));
  }
}
